# LightCrystal.gd - 光晶石道具脚本
# 《光影之间》- 第一章关键道具

extends Area2D

# 道具属性
@export var crystal_value := 1
@export var light_energy := 0.5
@export var collection_sound := "crystal_collect"
@export var respawn_time := 0.0  # 0表示不重生

# 状态
var is_collected := false
var player_nearby := false

# 节点引用
@onready var sprite: Sprite2D = $Sprite2D
@onready var collision: CollisionShape2D = $CollisionShape2D
@onready var detection_area: Area2D = $DetectionArea
@onready var light: PointLight2D = $Light2D
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var collect_effect: CPUParticles2D = $CollectEffect

# 信号
signal crystal_collected(crystal: Node2D, value: int)
signal player_near_crystal(crystal: Node2D)
signal player_left_crystal(crystal: Node2D)

func _ready():
	print("光晶石已生成")
	setup_crystal()
	
	# 注册到光照系统
	if LightingSystem:
		LightingSystem.register_light_source(light)

func _process(_delta):
	# 如果玩家靠近，增强光效
	if player_nearby and not is_collected:
		light.energy = light_energy * 1.5
	else:
		light.energy = light_energy

# 设置光晶石
func setup_crystal():
	# 设置光效
	light.energy = light_energy
	light.color = Color(1.0, 1.0, 0.8, 1.0)
	
	# 开始浮动动画
	if animation_player:
		animation_player.play("float")

# 玩家进入收集区域
func _on_body_entered(body):
	if body.has_method("collect_item") and not is_collected:
		collect_crystal(body)

# 玩家进入检测区域
func _on_detection_area_body_entered(body):
	if body.has_method("collect_item"):
		player_nearby = true
		player_near_crystal.emit(self)
		print("玩家靠近光晶石")

# 玩家离开检测区域
func _on_detection_area_body_exited(body):
	if body.has_method("collect_item"):
		player_nearby = false
		player_left_crystal.emit(self)
		print("玩家离开光晶石")

# 收集光晶石
func collect_crystal(player: Node):
	if is_collected:
		return
	
	is_collected = true
	print("光晶石被收集")
	
	# 播放收集特效
	play_collect_effect()
	
	# 通知玩家收集道具
	if player.has_method("collect_item"):
		player.collect_item("light_crystal", crystal_value)
	
	# 发送收集信号
	crystal_collected.emit(self, crystal_value)
	
	# 更新游戏状态
	if GameState:
		GameState.collect_item("light_crystal")
	
	# 从光照系统移除
	if LightingSystem:
		LightingSystem.unregister_light_source(light)
	
	# 隐藏道具
	hide_crystal()
	
	# 如果设置了重生时间，则重生
	if respawn_time > 0:
		await get_tree().create_timer(respawn_time).timeout
		respawn_crystal()
	else:
		# 延迟删除，确保特效播放完成
		await get_tree().create_timer(2.0).timeout
		queue_free()

# 播放收集特效
func play_collect_effect():
	# 播放粒子特效
	if collect_effect:
		collect_effect.emitting = true
	
	# 创建收集动画
	var tween = create_tween()
	tween.parallel().tween_property(sprite, "scale", Vector2(1.5, 1.5), 0.3)
	tween.parallel().tween_property(sprite, "modulate:a", 0.0, 0.5)
	tween.parallel().tween_property(light, "energy", 0.0, 0.5)
	
	# 播放音效（如果有音频系统）
	# AudioManager.play_sound(collection_sound)

# 隐藏光晶石
func hide_crystal():
	sprite.visible = false
	collision.disabled = true
	light.visible = false
	
	# 停止动画
	if animation_player:
		animation_player.stop()

# 重生光晶石
func respawn_crystal():
	is_collected = false
	player_nearby = false
	
	# 显示道具
	sprite.visible = true
	sprite.scale = Vector2.ONE
	sprite.modulate.a = 1.0
	collision.disabled = false
	light.visible = true
	light.energy = light_energy
	
	# 重新开始动画
	if animation_player:
		animation_player.play("float")
	
	# 重新注册到光照系统
	if LightingSystem:
		LightingSystem.register_light_source(light)
	
	print("光晶石重生")

# 设置光晶石属性
func set_crystal_properties(value: int, energy: float, respawn: float = 0.0):
	crystal_value = value
	light_energy = energy
	respawn_time = respawn
	
	if light:
		light.energy = light_energy

# 获取道具信息
func get_item_info() -> Dictionary:
	return {
		"type": "light_crystal",
		"value": crystal_value,
		"light_energy": light_energy,
		"is_collected": is_collected,
		"position": global_position
	}

# 强制收集（用于调试或特殊情况）
func force_collect():
	if not is_collected:
		collect_crystal(null)

# 设置可见性
func set_crystal_visible(visible: bool):
	sprite.visible = visible
	light.visible = visible
	collision.disabled = not visible

# 获取到玩家的距离
func get_distance_to_player() -> float:
	var player = get_tree().get_first_node_in_group("player")
	if player:
		return global_position.distance_to(player.global_position)
	return INF

# 检查是否在影视模式下可见
func is_visible_in_shadow_vision() -> bool:
	return true  # 光晶石在影视模式下总是可见的